package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.entity.Userauthinfo;
import com.yupi.springbootinit.service.UserauthinfoService;
import com.yupi.springbootinit.mapper.UserauthinfoMapper;
import com.yupi.springbootinit.utils.ThumbNailUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
* @author LENOVO
* @description 针对表【userauthinfo(认证信息)】的数据库操作Service实现
* @createDate 2024-11-11 17:45:41
*/
@Service
public class UserauthinfoServiceImpl extends ServiceImpl<UserauthinfoMapper, Userauthinfo>
    implements UserauthinfoService{


    @Value("${userInfoFilePath}")
    private String userInfoImgPath;
    @Override
    public String uploadUserInfoImage(MultipartFile file) {
        String fileName = null;
        String imageName = file.getOriginalFilename();
        if (imageName == null || imageName.isEmpty()) {
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String[] split = imageName.split("\\.");
        String imgType = split[split.length - 1];
        UUID randomUUID = UUID.randomUUID();
        fileName = randomUUID + "." + imgType;
        String filePath = userInfoImgPath + fileName;

        ThumbNailUtil.thumbnail(file, filePath,imgType);

        return fileName;
    }

    @Override
    public boolean deleteUserInfoImage(String fileName) {
        String filePath = userInfoImgPath + fileName;

        File imageFile = new File(filePath);
        if (imageFile.exists()) {
            boolean success = imageFile.delete();
            if (success) {
                return true;
            } else {
                throw  new BusinessException(ErrorCode.SYSTEM_ERROR,"删除图片失败");
            }
        } else {
            throw  new BusinessException(ErrorCode.PARAMS_ERROR,"图片不存在");
        }
    }
}




